package com.thememaker.lockstyle;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXTimePicker;
import com.thememake.common.Context;
import com.thememake.ui.R;
import com.thememaker.lockstyle.control.LayoutComponentUtils;
import com.thememaker.lockstyle.parsii.ExpressionUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Pair;

import java.time.LocalDate;
import java.time.LocalTime;

public class LockstyleCustomTimeDialog {
    private JFXDatePicker dateOverlay = new JFXDatePicker();
    private JFXTimePicker timeOverlay = new JFXTimePicker();
    private JFXComboBox<Label> comboBox;
    public static Pair<LocalDate,LocalTime> pair1 = new Pair<>(LocalDate.now(),LocalTime.now());
    public static boolean arrfim = false;
    public LockstyleCustomTimeDialog() {
    }

    public  void customTime(){
        Stage window = new Stage();
        window.setResizable(false);
        window.getIcons().add(new Image(
                getClass().getResourceAsStream(Context.getDrawable(R.drawable.launcher))));
        window.setTitle("自定义时间");
        window.initModality(Modality.APPLICATION_MODAL);
        window.setMinWidth(400);
        window.setMinHeight(400);
        window.setMaxWidth(400);
        window.setMaxHeight(400);
        VBox layout = new VBox(10);

        HBox dateHBox = new HBox(10);
        Font font = new Font(13);
        Label dateLabel = new Label("修改日期:  ");
        dateHBox.getChildren().add(dateLabel);
        dateHBox.getChildren().add(dateOverlay);

        HBox timeHBox = new HBox(10);

        Label timeLabel =  new Label("修改时间:  ");
        timeHBox.getChildren().add(timeLabel);

        Button affirm = new Button("确认");
        affirm.setStyle("-fx-text-fill:WHITE;-fx-background-color:#716A94;-fx-font-size:15px;");
        affirm.setPrefSize(100,20);
//        affirm.setId("diy-menuBar");
        Button cancel = new Button("取消");
        cancel.setStyle("-fx-text-fill:WHITE;-fx-background-color:#716A94;-fx-font-size:15px;");
        cancel.setPrefSize(100,20);
        if(dateOverlay.getValue()==null && timeOverlay.getValue()==null){
            dateOverlay.setStyle("-jfx-default-color: #716A94");
            dateOverlay.setValue(LocalDate.now());
            timeOverlay.setStyle("-jfx-default-color: #716A94");
            timeOverlay.setValue(LocalTime.now());
        }
        affirm.setOnAction(e -> {
                    if(comboBox.getSelectionModel().getSelectedIndex()==0){
                        LayoutComponentUtils.switcheTime = true;
                    }else if(comboBox.getSelectionModel().getSelectedIndex()==1){
                        LayoutComponentUtils.switcheTime = false;
                    }
             pair1 = new Pair<>(dateOverlay.getValue(), timeOverlay.getValue());
            dateOverlay.setValue(dateOverlay.getValue());
            timeOverlay.setValue(timeOverlay.getValue());
                    window.close();
            arrfim = true;
                }
        );
        cancel.setOnAction(e ->window.close());



        dateOverlay.setMaxSize(150,25);
        timeOverlay.setMaxSize(150,25);

//        timeHBox.getChildren().add(dateOverlay);
        timeHBox.getChildren().add(timeOverlay);
        Label label = new Label("切换时间制:");

        HBox switcheHBox = new HBox(10);

        ObservableList<Label> options = FXCollections.observableArrayList(new Label("12小时制"),new Label("24小时制"));
        comboBox = new JFXComboBox(options);
//        comboBox.setStyle("");
        comboBox.setMaxSize(150,20);
        if(LayoutComponentUtils.switcheTime){
            comboBox.getSelectionModel().select(0);
        }else {
            comboBox.getSelectionModel().select(1);
        }
        dateLabel.setFont(font);
        timeLabel.setFont(font);
        label.setFont(font);
        switcheHBox.getChildren().add(label);
        switcheHBox.getChildren().add(comboBox);
        dateHBox.setMargin(dateLabel,new Insets(5,5,5,50));
        timeHBox.setMargin(timeLabel,new Insets(5,5,5,50));
        switcheHBox.setMargin(label,new Insets(5,5,5,50));
        layout.getChildren().add(dateHBox);
        layout.getChildren().add(timeHBox);
        layout.getChildren().add(switcheHBox);
        layout.setMargin(dateHBox,new Insets(0,20,20,30));
        layout.setMargin(timeHBox,new Insets(20,20,20,30));
        layout.setMargin(switcheHBox,new Insets(20,20,20,30));
        HBox buttonHBox = new HBox(10);
        buttonHBox.getChildren().add(affirm);
        buttonHBox.getChildren().add(cancel);
        buttonHBox.setMargin(affirm,new Insets(0,0,0,40));
        buttonHBox.setMargin(cancel,new Insets(0,100,0,20));
        layout.getChildren().add(buttonHBox);
        buttonHBox.setPadding(new Insets(0,0,0,75));
        layout.setAlignment(Pos.CENTER);
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();
    }
}
